<template>
  <el-dialog :title="title" :width="width" :visible.sync="isShowVisible" center  :before-close="handle_close">
    <el-card>
      <el-form label-width="105px" :model="groupEdit" ref="groupEdit">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类名称:" prop="fname" required>
              <el-input v-model="groupEdit.fname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类编码:" prop="fnumber" required>
              <el-input v-model=" groupEdit.fnumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="上级分类:" prop="fparentid" id="fparentid">
          <el-select v-model="groupEdit.fparentid" disabled>
            <el-option v-for="item in groupList" :key="item.nodeId+''" :label="item.nodeName" :value="item.nodeId+''"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveGroup">保存</el-button>
      <el-button @click="handle_close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Group from "@/api/psyManage/Group";
export default {
  name: 'edit_group',
  props: {
    groupEdit:Object,
    width: { type: String, default: '40vw' },
    parentId: null,
    fid: null,
  },
  data () {

    return {
      title: '编辑分类',
      isShowVisible: true,
      groupData: {},
      groupList: [],
      isLoading: false,
      fparentid:'',
      rules:{
        fnumber:[{required:true,message:'分组编码不能为空',trigger:'blur'}],
        fname:[{required:true,message:'分组名称不能为空',trigger:'blur'}],
      }
    }
  },
  async created () {
    this.isLoading = true
    let http = new Group();
    let groupList=await http.queryGroupTree()
    this.groupList=groupList.data
    if (this.parentId){
      let groupData={}
      groupData.fparentid=this.parentId+''
      this.groupData=groupData
      console.log(this.groupData)
    }else {
      if (this.fid) {
        let groupData = await http.selectOne({fid: this.fid})
        this.groupData = groupData.data
        debugger
      this.groupEdit=groupData.data
        this.isLoading = false
      } else {
        this.isLoading = false
      }
    }

  },
  methods: {
    //提交
    saveGroup() {

      this.$refs['groupEdit'].validate((valid) => {
        if (valid) {
          const loading = this._loading();
          this.loading=true;
          const api = new Group();
          console.log(this.groupEdit)
          this.groupData=this.groupEdit
          api.update(this.groupData).then(res => {
            loading.close();
            this.$tie.showSuccess(res.message)
            this.$emit("close", {name: 'update', isRefresh: true});
          }).catch(e => loading.close())
        }
      })

    },

    handle_close() {

      this.$emit("close", {name: 'update', isRefresh: false});

    },
    //加载提示
    _loading() {
      return this.$loading({
        lock: true,
        text: '请耐心等待.....',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    }
  },

}
</script>

<style scoped>
</style>
